package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.ChangeStoreMessage;
import com.hongyuan.sellfitness.ui.home.beans.GroupClassBean;
import com.hongyuan.sellfitness.ui.home.beans.GroupClassTopBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class GroupClassViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    //当前选中项
    private int mSelectPosition = 0;

    private List<GroupClassTopBean> mList;

    //开始时间
    private String start_date = "";
    //结束时间
    private String end_date = "";

    public GroupClassViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<GroupClassTopItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<GroupClassTopItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_groupclass_top);

    //给RecyclerView添加ObservableList
    public ObservableList<GroupClassItemViewModel> contentList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<GroupClassItemViewModel> contentItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_groupclass);

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ChangeStoreMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(progressLoadBean -> {
                    curPage = FIRST_PAGE;

                    //更新下数据
                    setParams("os_ids",progressLoadBean.getOs_id());

                    requestData(Constants.GET_COURSE_SUPER_LIST);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    /*
    * 请求团课数据
    * */
    private void getCourses(){
        setParams("end_date",end_date).setParams("start_date",start_date)
                .setParams("city_code","3505");
        requestData(Constants.GET_COURSE_SUPER_LIST);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);
        if(code == Constants.GET_PLAN_DATE){
            mList = (List<GroupClassTopBean>) dataBean;

            for(int i = 0 ; i < mList.size() ; i++){
                if(i == 0){
                    mList.get(i).setSelect(true);

                    start_date = mList.get(i).getNow_day() + " 00:00";
                    end_date = mList.get(i).getNow_day() + " 23:59";
                }
                GroupClassTopItemViewModel itemViewModel = new GroupClassTopItemViewModel(this,mList.get(i));
                observableList.add(itemViewModel);
            }

            //请求一次
            getCourses();
        }

        if(code == Constants.GET_COURSE_SUPER_LIST){
            List<GroupClassBean.ListBean> list = ((GroupClassBean)dataBean).getList();

            //清除旧数据
            if(curPage == FIRST_PAGE){
                contentList.clear();
            }

            if(list != null && list.size() > 0){
                addBinding(list);
            }

            if(contentList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }
    }

    /*
     * 添加布局显示
     * */
    private void addBinding(List<GroupClassBean.ListBean> list){
        for(GroupClassBean.ListBean bean : list){
            GroupClassItemViewModel itemViewModel = new GroupClassItemViewModel(this,bean);
            contentList.add(itemViewModel);
        }
    }

    /*
    * 刷新下列表
    * */
    public void changeList(GroupClassTopItemViewModel itemViewModel){

        int newSelectPosition = getItemPosition(itemViewModel);
        if(newSelectPosition != mSelectPosition){
            //先改变选中项
            mList.get(mSelectPosition).setSelect(false);
            GroupClassTopItemViewModel oldSelect = new GroupClassTopItemViewModel(this,mList.get(mSelectPosition));
            observableList.set(mSelectPosition,oldSelect);

            mSelectPosition = getItemPosition(itemViewModel);
            mList.get(mSelectPosition).setSelect(true);
            GroupClassTopItemViewModel newSelect = new GroupClassTopItemViewModel(this,mList.get(mSelectPosition));
            observableList.set(mSelectPosition,newSelect);

            start_date = mList.get(mSelectPosition).getNow_day() + " 00:00";
            end_date = mList.get(mSelectPosition).getNow_day() + " 23:59";

            //改变设定为第一页
            curPage = FIRST_PAGE;
            //请求一次
            getCourses();
        }


    }

    /**
     * 获取条目下标
     *
     * @param itemViewModel
     * @return
     */
    public int getItemPosition(GroupClassTopItemViewModel itemViewModel) {
        return observableList.indexOf(itemViewModel);
    }
}
